package com.atl.controller.web;


import com.atl.common.BaseResponse;
import com.atl.common.DeleteRequest;
import com.atl.enity.po.Advertisement;
import com.atl.enity.web.dto.AdvertisementListDTO;
import com.atl.enity.web.vo.AdvertisementListVO;
import com.atl.execption.BusinessException;
import com.atl.service.IAdvertisementService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 
 * @since 2024-06-26
 */
@RestController
@RequestMapping("/web/advertisement")
@Api(tags = "广告管理")
public class AdvertisementController {

    @Resource
    private IAdvertisementService advertisementService;


    /**
     *
     * 查询
     *
     * @param advertisementListDTO
     * @return
     */
    @PostMapping("/list")
    @ApiOperation("广告搜索")
    public BaseResponse<Page<Advertisement>> search(@RequestBody AdvertisementListDTO advertisementListDTO) {
        // 处理接收到的数据
        Page<Advertisement> res  = advertisementService.getAll(advertisementListDTO);
        return BaseResponse.okResult(res);
    }


    /**
     *
     * 添加
     *
     * @param advertisement
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("添加广告信息")
    public BaseResponse add(@RequestBody Advertisement advertisement) {
        //添加信息
        if (advertisement.getCreatedAt() == null) {
            advertisement.setCreatedAt(LocalDateTime.now());
        }

        if (advertisement.getUpdatedAt() == null) {
            advertisement.setUpdatedAt(LocalDateTime.now());
        }


        boolean res = advertisementService.save(advertisement);
        return BaseResponse.okResult(res);
    }


    /**
     *
     * 删除
     *
     * @param deleteRequest
     * @param httpServletRequest
     * @return
     */
    @PostMapping("/del")
    @ApiOperation("广告删除")
    public BaseResponse del(@RequestBody DeleteRequest deleteRequest, HttpServletRequest httpServletRequest) {
        Long id = deleteRequest.getId();
        if (id <= 0) {
            throw new BusinessException(-1, "id错误");
        }

        //删除
        boolean res = advertisementService.removeById(id);

        return BaseResponse.okResult(res);
    }


    /**
     *
     * 修改
     *
     * @param advertisement
     * @param httpServletRequest
     * @return
     */
    @PostMapping("/update")
    @ApiOperation("广告修改")
    public BaseResponse update(@RequestBody Advertisement advertisement, HttpServletRequest httpServletRequest) {
        boolean res = advertisementService.updateById(advertisement);
        return BaseResponse.okResult(res);
    }


    /**
     *
     * 通过id获取
     *
     * @param deleteRequest
     * @param httpServletRequest
     * @return
     */
    @PostMapping("/get")
    @ApiOperation("根据id查询")
    public BaseResponse<Advertisement> getById(@RequestBody DeleteRequest deleteRequest, HttpServletRequest  httpServletRequest) {
        Long id = deleteRequest.getId();

        if (id <= 0) {
            throw new BusinessException(-1, "id错误");
        }

        Advertisement res = advertisementService.getById(id);
        return BaseResponse.okResult(res);
    }
}
